home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
simula
/
books
/
books.lha
/
kirkerud
/
weather.sim
< prev
Wrap
Text File
|
1993-08-16
|
11KB
|
311 lines
begin
! This is the program constructed in chapter 7 of
! Object Oriented Programming with Simula by Bj|rn Kirkerud;
! Declaration of data structure: ;
real array rain, min_temp, max_temp(1 : 365);
Boolean array sunny(1 : 365);
Boolean array data_known(1 : 365);
integer array days_in_month, month_start, month_end(1 : 12);
! Declarations of a variable to keep the latest command entered by the user: ;
character command;
! Declaration of a Boolean variable which is true if all data entered by the
! user have been written to file, false otherwise: ;
Boolean file_updated;
! Declarations of one procedure for each command: ;
procedure Give_help;
begin
User_message("The legal commands are: ");
User_message(" ?: Help (writes this text)");
User_message(" E: To enter data about a single day");
User_message(" Y: Writes data about a full year");
User_message(" P: Writes data about a month");
User_message(" D: Writes data about a single day");
User_message(" F: Writes all data to file ""meth.dta""");
User_message(" R: Reads data from file ""meth.dta""");
User_message(" H: Writes histograms");
User_message(" S: Produces some statistics");
User_message(" W: Finds the worst days in each month");
User_message(" Q: Quit (the program execution stops)");
end of Give_help;
procedure Enter_day;
begin integer m, d, day_in_year;
m := prompt_for_int("Which month (give its number)? ");
d := prompt_for_int("Which day in the month? ");
if not (1 <= m and then m <= 12 and then
1 <= d and then d <= days_in_month(m))
then Error_message("Illegal month or day")
else begin
day_in_year := month_start(m) - 1 + d;
rain(day_in_year) := prompt_for_real("How much rain that day? ");
min_temp(day_in_year) := prompt_for_real("Minimum temperature? ");
max_temp(day_in_year) := prompt_for_real("Maximum temperature? ");
sunny(day_in_year) := prompt_for_bool("Was the day sunny? ");
data_known(day_in_year) := true;
file_updated := false;
end;
end of Enter_day;
procedure Write_year; not_implemented("Write year");
procedure Write_month;
begin integer m, day_in_year; Boolean some_written;
m := prompt_for_int("Which month (give its number)? ");
if not (1 <= m and m <= 12) then Error_message("Illegal month")
else begin
outtext("Day Rain Minimum temp. Maximum temp. Sunny?"); outimage;
for day_in_year := month_start(m) step 1 until month_end(m) do
if data_known(day_in_year) then
begin
outint(day_in_year - month_start(m) + 1, 2);
outfix(rain(day_in_year), 1, 8);
outfix(min_temp(day_in_year), 1, 12);
outfix(max_temp(day_in_year), 1, 15); outtext(" ");
outtext(if sunny(day_in_year) then "yes" else "no");
outimage;
some_written := true;
end;
if not some_written then
begin outtext(" No data known for this month!"); outimage end;
end;
end of Write_month;
procedure Write_day; not_implemented("Write day");
procedure Write_to_file;
begin integer day_in_year, days_written;
inspect new outfile("meth.dta") do
begin
open(blanks(24));
for day_in_year := 1 step 1 until 365 do
if data_known(day_in_year) then
begin
outint(day_in_year, 3);
outfix(rain(day_in_year), 1, 7);
outfix(min_temp(day_in_year), 1, 6);
outfix(max_temp(day_in_year), 1, 6);
outint(if sunny(day_in_year) then 0 else 1, 2);
outimage;
days_written := days_written + 1;
end;
close;
end of inspect outfile;
if days_written > 0 then
begin
file_updated := true;
outtext("Data for "); outint(days_written, 3);
outtext(" days have been written to file ""meth.dta""."); outimage;
end
else User_message(" No data known. Nothing written to file.");
file_updated := true;
end of Write_to_file;
procedure Read_from_file;
begin
integer day_in_year, days_read;
inspect new infile("meth.dta") do
if open(blanks(24)) then
begin
inimage;
while not endfile do
begin
day_in_year := inint;
rain(day_in_year) := inreal;
min_temp(day_in_year) := inreal;
max_temp(day_in_year) := inreal;
sunny(day_in_year) := inint = 1;
data_known(day_in_year) := true;
days_read := days_read + 1;
inimage;
end;
close;
end;
if days_read > 0 then
begin
outtext("Data for "); outint(days_read, 3);
outtext(" days have been read from file ""meth.dta"".");
outimage;
end
else User_message(" No data found on file ""meth.dta""");
file_updated := true;
end of Read_from_file;
procedure Write_histograms; not_implemented("Write histograms");
procedure Produce_statistics; not_implemented("Produce statistics");
procedure Find_worst_days;
begin
integer m, day1, day2;
Boolean mintemp_found, maxrain_found;
real minmintemp, maxrain;
outtext("The worst days:"); outimage;
for m := 1 step 1 until 12 do
begin
min_in_tab(min_temp, data_known,
month_start(m), month_end(m),
mintemp_found, minmintemp, day1);
max_in_tab(rain, data_known,
month_start(m), month_end(m),
maxrain_found, maxrain, day2);
outtext("In month "); outint(m, 2); outchar(':'); outimage;
if mintemp_found then
begin
outtext(" It was coldest on day");
outint(day1 - month_start(m) + 1, 3);
outtext(" with minimum temperature"); outfix(minmintemp, 1, 5);
outtext(" degrees."); outimage;
end;
if maxrain_found then
begin
outtext(" It rained most on day");
outint(day2 - month_start(m) + 1, 3);
outtext(" with "); outfix(maxrain, 1, 5);
outtext(" mm."); outimage;
end;
if not (mintemp_found or maxrain_found) then
User_message(" No data known for this month!");
end;
end of Find_worst_days;
! Declarations of some auxiliary procedures: ;
procedure Unknown_command(c); character c;
begin
outtext(" You gave the command '"); outchar(c);
outtext("'. This is not among the legal commands."); outimage;
outtext(" Type ? if you don't remember the legal commands"); outimage;
end of Unknown command;
procedure not_implemented(action_name); text action_name;
begin
outtext(" You gave the command """); outtext(action_name);
outtext(""". This has not been implemented yet!"); outimage;
outtext(" Try another command!"); outimage;
end;
procedure Initialize_months;
begin integer m;
days_in_month( 1) := 31; days_in_month( 2) := 28;
days_in_month( 3) := 31; days_in_month( 4) := 30;
days_in_month( 5) := 31; days_in_month( 6) := 30;
days_in_month( 7) := 31; days_in_month( 8) := 31;
days_in_month( 9) := 30; days_in_month(10) := 31;
days_in_month(11) := 30; days_in_month(12) := 31;
month_start(1) := 1;
for m := 2 step 1 until 12 do
month_start(m) := month_start(m-1) + days_in_month(m-1);
for m := 1 step 1 until 12 do
month_end(m) := month_start(m) + days_in_month(m) - 1;
end of Initialize_months;
! Declarations of some procedures of general utility: ;
procedure User_message(message); text message;
begin outtext(message); outimage end;
procedure Error_message(message); text message;
begin outtext("** Error **" & message); outimage end;
character procedure prompt_for_char(prompt); text prompt;
begin
outtext(prompt); breakoutimage;
inimage; prompt_for_char := inchar;
end of prompt_for_char;
Boolean procedure prompt_for_bool(prompt); text prompt;
begin character c;
outtext(prompt); breakoutimage;
inimage; c := inchar;
prompt_for_bool := c = 'y' or c = 'Y';
end of prompt_for_bool;
real procedure prompt_for_real(prompt); text prompt;
begin
outtext(prompt); breakoutimage;
inimage; prompt_for_real := inreal;
end of prompt_for_real;
integer procedure prompt_for_int(prompt); text prompt;
begin
outtext(prompt); breakoutimage;
inimage; prompt_for_int := inint;
end of prompt_for_int;
procedure min_in_tab(tab, val_ok, lowind, highind,
min_val_found, min_val, min_ind);
name min_val_found, min_val, min_ind;
real array tab; Boolean array val_ok;
Boolean min_val_found; integer lowind, highind; real min_val, min_ind;
begin integer ind;
min_val := maxreal; min_val_found := false;
for ind := lowind step 1 until highind do
if val_ok(ind) and then tab(ind) < min_val then
begin min_val := tab(ind); min_ind := ind; min_val_found := true end;
end of min_in_tab;
procedure max_in_tab(tab, val_ok, lowind, highind,
max_val_found, max_val, max_ind);
name max_val_found, max_val, max_ind;
real array tab; Boolean array val_ok;
Boolean max_val_found; integer lowind, highind; real max_val, max_ind;
begin integer ind;
max_val := minreal; max_val_found := false;
for ind := lowind step 1 until highind do
if val_ok(ind) and then tab(ind) > max_val then
begin max_val := tab(ind); max_ind := ind; max_val_found := true end;
end of max_in_tab;
! That was the end of the declarations.
! Now come the imperatives of the program: ;
Initialize_months;
file_updated := true;
command := prompt_for_char("Type your first command (type ? for help) > ");
while command ne 'Q' do
begin
if command = '?' then Give_help else
if command = 'E' then Enter_day else
if command = 'Y' then Write_year else
if command = 'P' then Write_month else
if command = 'D' then Write_day else
if command = 'F' then Write_to_file else
if command = 'R' then Read_from_file else
if command = 'H' then Write_histograms else
if command = 'S' then Produce_statistics else
if command = 'W' then Find_worst_days
else Unknown_command(command);
command := prompt_for_char("Your next command > ");
end;
if not file_updated then
begin
User_message("Some of your data has not been written to file.");
if prompt_for_bool("Do you want them written to file? ")
then Write_to_file;
end;
User_message("Bye");
end